3 use Wikimedia\ScopedCallback
;
4 use Wikimedia\TestingAccessWrapper
;
7 * @author Matthias Mullie <mmullie@wikimedia.org>
11 class BagOStuffTest
extends MediaWikiTestCase
{
15 const TEST_KEY
= 'test';
17 protected function setUp() {
20 // type defined through parameter
21 if ( $this->getCliArg( 'use-bagostuff' ) !== null ) {
22 global $wgObjectCaches;
24 $id = $this->getCliArg( 'use-bagostuff' );
25 $this->cache
= ObjectCache
::newFromParams( $wgObjectCaches[$id] );
27 // no type defined - use simple hash
28 $this->cache
= new HashBagOStuff
;
31 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) );
32 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) . ':lock' );
36 * @covers MediumSpecificBagOStuff::makeGlobalKey
37 * @covers MediumSpecificBagOStuff::makeKeyInternal
39 public function testMakeKey() {
40 $cache = new HashBagOStuff();
42 $localKey = $cache->makeKey( 'first', 'second', 'third' );
43 $globalKey = $cache->makeGlobalKey( 'first', 'second', 'third' );
45 $this->assertStringMatchesFormat(
46 '%Sfirst%Ssecond%Sthird%S',
48 'Local key interpolates parameters'
51 $this->assertStringMatchesFormat(
52 'global%Sfirst%Ssecond%Sthird%S',
54 'Global key interpolates parameters and contains global prefix'
57 $this->assertNotEquals(
60 'Local key and global key with same parameters should not be equal'
63 $this->assertNotEquals(
64 $cache->makeKeyInternal( 'prefix', [ 'a', 'bc:', 'de' ] ),
65 $cache->makeKeyInternal( 'prefix', [ 'a', 'bc', ':de' ] )
70 * @covers MediumSpecificBagOStuff::merge
71 * @covers MediumSpecificBagOStuff::mergeViaCas
73 public function testMerge() {
74 $key = $this->cache
->makeKey( self
::TEST_KEY
);
77 $casRace = false; // emulate a race
78 $callback = function ( BagOStuff
$cache, $key, $oldVal ) use ( &$calls, &$casRace ) {
82 $cache->set( $key, 'conflict', 5 );
85 return ( $oldVal === false ) ?
'merged' : $oldVal . 'merged';
88 // merge on non-existing value
89 $merged = $this->cache
->merge( $key, $callback, 5 );
90 $this->assertTrue( $merged );
91 $this->assertEquals( 'merged', $this->cache
->get( $key ) );
93 // merge on existing value
94 $merged = $this->cache
->merge( $key, $callback, 5 );
95 $this->assertTrue( $merged );
96 $this->assertEquals( 'mergedmerged', $this->cache
->get( $key ) );
101 $this->cache
->merge( $key, $callback, 5, 1 ),
102 'Non-blocking merge (CAS)'
105 if ( $this->cache
instanceof MultiWriteBagOStuff
) {
106 $wrapper = TestingAccessWrapper
::newFromObject( $this->cache
);
107 $this->assertEquals( count( $wrapper->caches
), $calls );
109 $this->assertEquals( 1, $calls );
114 * @covers MediumSpecificBagOStuff::changeTTL
116 public function testChangeTTLRenew() {
117 $now = microtime( true ); // need real time
118 $this->cache
->setMockTime( $now );
120 $key = $this->cache
->makeKey( self
::TEST_KEY
);
123 $this->cache
->add( $key, $value, 60 );
124 $this->assertEquals( $value, $this->cache
->get( $key ) );
125 $this->assertTrue( $this->cache
->changeTTL( $key, 120 ) );
126 $this->assertTrue( $this->cache
->changeTTL( $key, 120 ) );
127 $this->assertTrue( $this->cache
->changeTTL( $key, 0 ) );
128 $this->assertEquals( $this->cache
->get( $key ), $value );
130 $this->cache
->delete( $key );
131 $this->assertFalse( $this->cache
->changeTTL( $key, 15 ) );
135 * @covers MediumSpecificBagOStuff::changeTTL
137 public function testChangeTTLExpireRel() {
138 $now = microtime( true ); // need real time
139 $this->cache
->setMockTime( $now );
141 $key = $this->cache
->makeKey( self
::TEST_KEY
);
144 $this->cache
->add( $key, $value, 5 );
145 $this->assertTrue( $this->cache
->changeTTL( $key, -3600 ) );
146 $this->assertFalse( $this->cache
->get( $key ) );
150 * @covers MediumSpecificBagOStuff::changeTTL
152 public function testChangeTTLExpireAbs() {
153 $now = microtime( true ); // need real time
154 $this->cache
->setMockTime( $now );
156 $key = $this->cache
->makeKey( self
::TEST_KEY
);
159 $this->cache
->add( $key, $value, 5 );
160 $this->assertTrue( $this->cache
->changeTTL( $key, $now - 3600 ) );
161 $this->assertFalse( $this->cache
->get( $key ) );
165 * @covers MediumSpecificBagOStuff::changeTTLMulti
167 public function testChangeTTLMulti() {
169 $this->cache
->setMockTime( $now );
171 $key1 = $this->cache
->makeKey( 'test-key1' );
172 $key2 = $this->cache
->makeKey( 'test-key2' );
173 $key3 = $this->cache
->makeKey( 'test-key3' );
174 $key4 = $this->cache
->makeKey( 'test-key4' );
177 $this->cache
->deleteMulti( [ $key1, $key2, $key3, $key4 ] );
179 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3 ], 30 );
180 $this->assertFalse( $ok, "No keys found" );
181 $this->assertFalse( $this->cache
->get( $key1 ) );
182 $this->assertFalse( $this->cache
->get( $key2 ) );
183 $this->assertFalse( $this->cache
->get( $key3 ) );
185 $ok = $this->cache
->setMulti( [ $key1 => 1, $key2 => 2, $key3 => 3 ] );
186 $this->assertTrue( $ok, "setMulti() succeeded" );
189 count( $this->cache
->getMulti( [ $key1, $key2, $key3 ] ) ),
190 "setMulti() succeeded via getMulti() check"
193 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3 ], 300 );
194 $this->assertTrue( $ok, "TTL bumped for all keys" );
195 $this->assertEquals( 1, $this->cache
->get( $key1 ) );
196 $this->assertEquals( 2, $this->cache
->get( $key2 ) );
197 $this->assertEquals( 3, $this->cache
->get( $key3 ) );
199 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3, $key4 ], 300 );
200 $this->assertFalse( $ok, "One key missing" );
201 $this->assertEquals( 1, $this->cache
->get( $key1 ), "Key still live" );
203 $now = microtime( true ); // real time
204 $ok = $this->cache
->setMulti( [ $key1 => 1, $key2 => 2, $key3 => 3 ] );
205 $this->assertTrue( $ok, "setMulti() succeeded" );
207 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3 ], $now +
86400 );
208 $this->assertTrue( $ok, "Expiry set for all keys" );
209 $this->assertEquals( 1, $this->cache
->get( $key1 ), "Key still live" );
211 $this->assertEquals( 2, $this->cache
->incr( $key1 ) );
212 $this->assertEquals( 3, $this->cache
->incr( $key2 ) );
213 $this->assertEquals( 4, $this->cache
->incr( $key3 ) );
216 $this->cache
->deleteMulti( [ $key1, $key2, $key3, $key4 ] );
220 * @covers MediumSpecificBagOStuff::add
222 public function testAdd() {
223 $key = $this->cache
->makeKey( self
::TEST_KEY
);
224 $this->assertFalse( $this->cache
->get( $key ) );
225 $this->assertTrue( $this->cache
->add( $key, 'test', 5 ) );
226 $this->assertFalse( $this->cache
->add( $key, 'test', 5 ) );
230 * @covers MediumSpecificBagOStuff::get
232 public function testGet() {
233 $value = [ 'this' => 'is', 'a' => 'test' ];
235 $key = $this->cache
->makeKey( self
::TEST_KEY
);
236 $this->cache
->add( $key, $value, 5 );
237 $this->assertEquals( $this->cache
->get( $key ), $value );
241 * @covers MediumSpecificBagOStuff::get
242 * @covers MediumSpecificBagOStuff::set
243 * @covers MediumSpecificBagOStuff::getWithSetCallback
245 public function testGetWithSetCallback() {
247 $cache = new HashBagOStuff( [] );
248 $cache->setMockTime( $now );
249 $key = $cache->makeKey( self
::TEST_KEY
);
251 $this->assertFalse( $cache->get( $key ), "No value" );
253 $value = $cache->getWithSetCallback(
259 return 'hello kitty';
263 $this->assertEquals( 'hello kitty', $value );
264 $this->assertEquals( $value, $cache->get( $key ), "Value set" );
268 $this->assertFalse( $cache->get( $key ), "Value expired" );
272 * @covers MediumSpecificBagOStuff::incr
274 public function testIncr() {
275 $key = $this->cache
->makeKey( self
::TEST_KEY
);
276 $this->cache
->add( $key, 0, 5 );
277 $this->cache
->incr( $key );
279 $actualValue = $this->cache
->get( $key );
280 $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
284 * @covers MediumSpecificBagOStuff::incrWithInit
286 public function testIncrWithInit() {
287 $key = $this->cache
->makeKey( self
::TEST_KEY
);
288 $val = $this->cache
->incrWithInit( $key, 0, 1, 3 );
289 $this->assertEquals( 3, $val, "Correct init value" );
291 $val = $this->cache
->incrWithInit( $key, 0, 1, 3 );
292 $this->assertEquals( 4, $val, "Correct init value" );
293 $this->cache
->delete( $key );
295 $val = $this->cache
->incrWithInit( $key, 0, 5 );
296 $this->assertEquals( 5, $val, "Correct init value" );
300 * @covers MediumSpecificBagOStuff::getMulti
302 public function testGetMulti() {
303 $value1 = [ 'this' => 'is', 'a' => 'test' ];
304 $value2 = [ 'this' => 'is', 'another' => 'test' ];
305 $value3 = [ 'testing a key that may be encoded when sent to cache backend' ];
306 $value4 = [ 'another test where chars in key will be encoded' ];
308 $key1 = $this->cache
->makeKey( 'test-1' );
309 $key2 = $this->cache
->makeKey( 'test-2' );
310 // internally, MemcachedBagOStuffs will encode to will-%25-encode
311 $key3 = $this->cache
->makeKey( 'will-%-encode' );
312 $key4 = $this->cache
->makeKey(
313 'flowdb:flow_ref:wiki:by-source:v3:Parser\'s_"broken"_+_(page)_&_grill:testwiki:1:4.7'
317 $this->cache
->delete( $key1 );
318 $this->cache
->delete( $key2 );
319 $this->cache
->delete( $key3 );
320 $this->cache
->delete( $key4 );
322 $this->cache
->add( $key1, $value1, 5 );
323 $this->cache
->add( $key2, $value2, 5 );
324 $this->cache
->add( $key3, $value3, 5 );
325 $this->cache
->add( $key4, $value4, 5 );
328 [ $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ],
329 $this->cache
->getMulti( [ $key1, $key2, $key3, $key4 ] )
333 $this->cache
->delete( $key1 );
334 $this->cache
->delete( $key2 );
335 $this->cache
->delete( $key3 );
336 $this->cache
->delete( $key4 );
340 * @covers MediumSpecificBagOStuff::setMulti
341 * @covers MediumSpecificBagOStuff::deleteMulti
343 public function testSetDeleteMulti() {
345 $this->cache
->makeKey( 'test-1' ) => 'Siberian',
346 $this->cache
->makeKey( 'test-2' ) => [ 'Huskies' ],
347 $this->cache
->makeKey( 'test-3' ) => [ 'are' => 'the' ],
348 $this->cache
->makeKey( 'test-4' ) => (object)[ 'greatest' => 'animal' ],
349 $this->cache
->makeKey( 'test-5' ) => 4,
350 $this->cache
->makeKey( 'test-6' ) => 'ever'
353 $this->assertTrue( $this->cache
->setMulti( $map ) );
356 $this->cache
->getMulti( array_keys( $map ) )
359 $this->assertTrue( $this->cache
->deleteMulti( array_keys( $map ) ) );
363 $this->cache
->getMulti( array_keys( $map ), BagOStuff
::READ_LATEST
)
367 $this->cache
->getMulti( array_keys( $map ) )
372 * @covers MediumSpecificBagOStuff::get
373 * @covers MediumSpecificBagOStuff::getMulti
374 * @covers MediumSpecificBagOStuff::merge
375 * @covers MediumSpecificBagOStuff::delete
377 public function testSetSegmentable() {
378 $key = $this->cache
->makeKey( self
::TEST_KEY
);
380 $small = wfRandomString( 32 );
381 // 64 * 8 * 32768 = 16777216 bytes
382 $big = str_repeat( wfRandomString( 32 ) . '-' . wfRandomString( 32 ), 32768 );
384 $callback = function ( $cache, $key, $oldValue ) {
385 return $oldValue . '!';
388 $cases = [ 'tiny' => $tiny, 'small' => $small, 'big' => $big ];
389 foreach ( $cases as $case => $value ) {
390 $this->cache
->set( $key, $value, 10, BagOStuff
::WRITE_ALLOW_SEGMENTS
);
391 $this->assertEquals( $value, $this->cache
->get( $key ), "get $case" );
392 $this->assertEquals( $value, $this->cache
->getMulti( [ $key ] )[$key], "get $case" );
395 $this->cache
->merge( $key, $callback, 5, 1, BagOStuff
::WRITE_ALLOW_SEGMENTS
),
400 $this->cache
->get( $key ),
405 $this->cache
->getMulti( [ $key ] )[$key],
409 $this->assertTrue( $this->cache
->deleteMulti( [ $key ] ), "delete $case" );
410 $this->assertFalse( $this->cache
->get( $key ), "deleted $case" );
411 $this->assertEquals( [], $this->cache
->getMulti( [ $key ] ), "deletd $case" );
413 $this->cache
->set( $key, "@$value", 10, BagOStuff
::WRITE_ALLOW_SEGMENTS
);
414 $this->assertEquals( "@$value", $this->cache
->get( $key ), "get $case" );
416 $this->cache
->delete( $key, BagOStuff
::WRITE_PRUNE_SEGMENTS
),
419 $this->assertFalse( $this->cache
->get( $key ), "pruned $case" );
420 $this->assertEquals( [], $this->cache
->getMulti( [ $key ] ), "pruned $case" );
423 $this->cache
->set( $key, 666, 10, BagOStuff
::WRITE_ALLOW_SEGMENTS
);
425 $this->assertEquals( 666, $this->cache
->get( $key ) );
426 $this->assertEquals( 667, $this->cache
->incr( $key ) );
427 $this->assertEquals( 667, $this->cache
->get( $key ) );
429 $this->assertEquals( 664, $this->cache
->decr( $key, 3 ) );
430 $this->assertEquals( 664, $this->cache
->get( $key ) );
432 $this->assertTrue( $this->cache
->delete( $key ) );
433 $this->assertFalse( $this->cache
->get( $key ) );
437 * @covers MediumSpecificBagOStuff::getScopedLock
439 public function testGetScopedLock() {
440 $key = $this->cache
->makeKey( self
::TEST_KEY
);
441 $value1 = $this->cache
->getScopedLock( $key, 0 );
442 $value2 = $this->cache
->getScopedLock( $key, 0 );
444 $this->assertType( ScopedCallback
::class, $value1, 'First call returned lock' );
445 $this->assertNull( $value2, 'Duplicate call returned no lock' );
449 $value3 = $this->cache
->getScopedLock( $key, 0 );
450 $this->assertType( ScopedCallback
::class, $value3, 'Lock returned callback after release' );
453 $value1 = $this->cache
->getScopedLock( $key, 0, 5, 'reentry' );
454 $value2 = $this->cache
->getScopedLock( $key, 0, 5, 'reentry' );
456 $this->assertType( ScopedCallback
::class, $value1, 'First reentrant call returned lock' );
457 $this->assertType( ScopedCallback
::class, $value1, 'Second reentrant call returned lock' );
461 * @covers MediumSpecificBagOStuff::__construct
462 * @covers MediumSpecificBagOStuff::trackDuplicateKeys
464 public function testReportDupes() {
465 $logger = $this->createMock( Psr\Log\NullLogger
::class );
466 $logger->expects( $this->once() )
467 ->method( 'warning' )
468 ->with( 'Duplicate get(): "{key}" fetched {count} times', [
473 $cache = new HashBagOStuff( [
474 'reportDupes' => true,
475 'asyncHandler' => 'DeferredUpdates::addCallableUpdate',
478 $cache->get( 'foo' );
479 $cache->get( 'bar' );
480 $cache->get( 'foo' );
482 DeferredUpdates
::doUpdates();
486 * @covers MediumSpecificBagOStuff::lock()
487 * @covers MediumSpecificBagOStuff::unlock()
489 public function testLocking() {
491 $this->assertTrue( $this->cache
->lock( $key ) );
492 $this->assertFalse( $this->cache
->lock( $key ) );
493 $this->assertTrue( $this->cache
->unlock( $key ) );
496 $this->assertTrue( $this->cache
->lock( $key2, 5, 5, 'rclass' ) );
497 $this->assertTrue( $this->cache
->lock( $key2, 5, 5, 'rclass' ) );
498 $this->assertTrue( $this->cache
->unlock( $key2 ) );
499 $this->assertTrue( $this->cache
->unlock( $key2 ) );
502 public function tearDown() {
503 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) );
504 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) . ':lock' );